"""
Case Type   : DSS元数据修复工具
Case Name   : dsstbox ssrepair参数vol_path验证
Create At   : 2025/04
Owner       : haomeng
Description :
    1、查看当前卷名
    2、修改元数据且指定有效的卷名
    3、将元数据值恢复为原始值
    4、修改元数据且指定无效的卷名
    5、修改元数据且指定有效但不可访问的卷名
Expect      :
    1、查看成功
    2、修改成功
    3、恢复成功
    4、修改失败
    5、修改失败
History     :
"""

import os
import time
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DSSrepairtools(unittest.TestCase):

    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.user_node = Node('PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()

        self.vol_path = os.path.join(macro.DSS_INSTANCE_PATH, 'cfg/dss_vg_conf.ini')
        self.param = ['volume_count', 'version', 'au_size']
        self.default_value = []

    def test_dsstbox(self):
        text = '-----step1:查看当前卷名;  expect:查看成功-----'
        self.log.info(text)

        res = self.user_node.sh(f"cat {self.vol_path} | awk 'NR==1'").result()
        self.log.info(res)
        vol_name = res.split(':')[0]
        self.vol_value = res.split(':')[1]
        self.log.info(f'vol_name的值是：{vol_name}, vol_value的值是：{self.vol_value}')
        self.assertNotEqual('', vol_name, '查看当前卷名失败')

        self.log.info('-----查看待修改元素的初始值-----')
        cmd = f'source {macro.DB_ENV_PATH};dsscmd showdisk -g {vol_name} -s core_ctrl'
        self.log.info(cmd)
        res = self.user_node.sh(cmd).result()
        self.log.info(res)

        for param in self.param:
            value = res.split(param)[-1].split('=')[1].split(' ')[1].strip()
            self.log.info(value)
            self.default_value.append(value)
        self.log.info(self.default_value)

        text = '-----step2:修改元数据且指定有效的卷名;  expect:修改成功-----'
        self.log.info(text)

        cmd1 = f"dsstbox ssrepair -v {self.vol_value} -t core_ctrl -k '{self.param[0]}=2,{self.param[1]}=3896,{self.param[0]}=8192'"
        self.log.info(cmd1)
        execute_cmd1 = f'''source {macro.DB_ENV_PATH};yes|{cmd1}'''
        self.log.info(execute_cmd1)
        res1 = self.user_node.sh(execute_cmd1).result()
        self.log.info(res1)
        self.assertIn('Succeed to execute repair meta info', res1, '修改元数据失败')

        text = '-----step3:将元数据值恢复为原始值;  expect:恢复成功-----'
        self.log.info(text)

        cmd2 = f"dsstbox ssrepair -v {self.vol_value} -t core_ctrl " \
               f"-k '{self.param[0]}={self.default_value[0]},{self.param[1]}={self.default_value[1]}," \
               f"{self.param[2]}=4096'"
        self.log.info(cmd2)
        execute_cmd2 = f'''source {macro.DB_ENV_PATH};yes|{cmd2}'''
        self.log.info(execute_cmd2)
        res2 = self.user_node.sh(execute_cmd2).result()
        self.log.info(res2)
        self.assertIn('Succeed to execute repair meta info', res2, '恢复元数据c初始值失败')

        text = '-----step4:修改元数据且指定无效的卷名;  expect:修改失败-----'
        self.log.info(text)

        cmd3 = f"dsstbox ssrepair -v {self.vol_value}_no_exists -t core_ctrl -k '{self.param[0]}=8192,{self.param[1]}=2,{self.param[2]}=3896'"
        self.log.info(cmd3)
        execute_cmd3 = f'''source {macro.DB_ENV_PATH};yes|{cmd3}'''
        self.log.info(execute_cmd3)
        res3 = self.user_node.sh(execute_cmd3).result()
        self.log.info(res3)
        self.assertIn('ssrepair execute failed', res3, '修改元数据成功')

        text = '-----step5:修改元数据且指定有效但不可访问的卷名;  expect:修改失败-----'
        self.log.info(text)

        permit = self.user_node.sh(f"chmod 000 {self.vol_value};ls -al {self.vol_value}").result()
        self.log.info(permit)

        cmd4 = f"dsstbox ssrepair -v {self.vol_value} -t core_ctrl -k '{self.param[0]}=8192,{self.param[1]}=2,{self.param[2]}=3896'"
        self.log.info(cmd4)
        execute_cmd4 = f'''source {macro.DB_ENV_PATH};yes|{cmd4}'''
        self.log.info(execute_cmd4)
        res4 = self.user_node.sh(execute_cmd4).result()
        self.log.info(res4)
        self.assertIn(f'Open volume {self.vol_value} failed', res4, '修改元数据成功')

    def tearDown(self):
        self.log.info('-----恢复环境权限,恢复集群状态-----')
        recovery = self.user_node.sh(f"chmod 660 {self.vol_value};ls -al {self.vol_value}").result()
        self.log.info(recovery)
        self.assertIn('rw', recovery, '恢复权限失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')
